<template>
<div v-if="switchOpen" class="class-user" v-loading.fullscreen.lock="loading" element-loading-text="拼命加载中">
    <!--搜索框 -->
    <search></search>
    <!-- 用户列表 -->
    <component-table :tableData='tableData' :roleList="roleList"></component-table>

    <!-- 分页 -->
    <common-pageination :pageNo="pageNo" :total="total" :pageSize="pageSize">
    </common-pageination>
</div>
</template>

<script>
import Api from '@/api/Api'
import axios from 'axios'
import {
    formatDate
} from '@/utils/Utils'
import componentTable from './component/Table'
import Bus from './component/Bus'
import Search from './component/Search'
import CommonPageination from './component/Pagination'
export default {
    components: {
        componentTable,
        Search,
        CommonPageination
    },
    data() {
        return {
            //用户列表数据
            tableData: [],
            //角色授权数据
            roleList: [],
            //发送绑定的数据
            sendData: [],
            //当前页面
            pageNo: 1,
            //长度
            pageSize: 10,
            //总数
            total: 1,
            //授权角色id
            roleId: '',
            loading: false,
            switchOpen: false
        }
    },
    methods: {
        // 获取用户列表数据
        getUserList(val, likeName) {
            let state = this.$store.state
            let params = {
                pageNo: val || 1,
                likeName: likeName,
                organizationId: state.user.organizationId
            }
            Api.user.find(params).then(res => {
                if (res.data.code === 0) {
                    let resData = res.data.data
                    this.switchOpen = true
                    this.pageNo = resData.data.pageNum
                    this.tableData = resData.data.list
                    this.total = resData.data.total
                    this.pageSize = resData.data.pageSize
                    this.roleList = resData.roles
                }else{
                return this.$message.error(res.data.msg)
              }
            }).catch(error => {
                console.log(error)
            })
        },
    },
    mounted() {
        this.loading = true
        this.getUserList()
        this.$nextTick(() => {
            setTimeout(() => {
                this.loading = false
            }, 500);
        })
        // 监听bus中loading状态
        Bus.$on('open-v-loading', (msg) => {
            this.loading = true
        })
        Bus.$on('close-v-loading', (msg, type) => {
            let types = type == undefined ? 'success' : type
            this.getUserList(this.pageNo)
            setTimeout(() => {
                this.loading = false
                this.$message({
                    type: types,
                    message: msg
                });
            }, 400);
        })
        Bus.$on('getUserList', (val, likeName) => {
            // 开启login
            this.loading = true
            this.getUserList(val, likeName)
            setTimeout(() => {
                this.loading = false
            }, 400);
        })
    }
}
</script>
